package com.sxjh.common;

import io.swagger.annotations.ApiModelProperty;

/**
 * 返回结果对象
 * @author zixin
 * @since 2020-01-09 15:24
 */

public class Result<T> {

  @ApiModelProperty("状态码")
  private final Integer code;

  @ApiModelProperty("信息")
  private final String msg;

  @ApiModelProperty("数据")
  private final T data;

  private Result(Integer code, String msg, T data) {
    this.code = code;
    this.msg = msg;
    this.data = data;
  }

  private static <T> Result<T> ok(Integer code, T data) {
    return new Result<>(code,null, data);
  }

  public static Result<Void> ok() {
    return ok(200, null);
  }

  public static <T> Result<T> ok(T data) {
    return ok(200, data);
  }

  public static Result<Void> ok0() {
    return ok(0, null);
  }

  public static <T>Result<T> err(Integer code, String msg) {
    return new Result<>(code, msg, null);
  }

  public static <T>Result<T> err(String msg) {
    return err(500, msg);
  }

  public static <T>Result<T> err() {
    return err(null);
  }

  // 自定义返回
  public static <T>Result<T> cus(Integer code ,String msg, T data) {
    return new Result<>(code, msg, data);
  }

  public static Result<Void> cus(Integer code ,String msg) {
    return cus(code, msg, null);
  }

  public Integer getCode() {
    return this.code;
  }

  public String getMsg() {
    return this.msg;
  }

  public T getData() {
    return this.data;
  }

  @Override
  public String toString() {
    return "TestResult{" +
            "code=" + code +
            ", msg='" + msg + '\'' +
            ", data=" + data +
            '}';
  }
}
